package cn.site.modules.sys.entity;

import cn.site.core.utils.IdGen;
import com.google.common.collect.Lists;
import org.hibernate.annotations.*;
import org.hibernate.annotations.Cache;
import org.hibernate.validator.constraints.Length;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;

/**
 * 菜单Entity
 *
 * Created by ZGJ on 2015/3/15.
 */
@Entity
@Table(name = "sys_menu")
@DynamicInsert
@DynamicUpdate
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Menu implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@Id
    private String id;
    @Length(min=1, max=100)
    @Column(name = "name", length = 100)
    private String name;        // 名称
    @Length(min=0, max=255)
    @Column(name = "href", length = 255)
    private String href; 	    // 链接
    @Length(min=0, max=20)
    @Column(name = "target", length = 20)
    private String target; 	    // 目标（ mainFrame、_blank、_self、_parent、_top）
    @Length(min=0, max=100)
    @Column(name = "icon", length = 100)
    private String icon; 	    // 图标
    @NotNull
    @Column(name = "sort", length = 1)
    private Integer sort; 	    // 排序
    @Length(min=1, max=1)
    @Column(name = "is_show", length = 1)
    private String isShow; 	    // 是否在菜单中显示（1：显示；0：不显示）
    @Length(min=0, max=200)
    @Column(name = "permission", length = 200)
    private String permission;  // 权限标识

    @OneToMany(mappedBy = "parent", fetch=FetchType.LAZY)
    @OrderBy(value="sort") @Fetch(FetchMode.SUBSELECT)
    @NotFound(action = NotFoundAction.IGNORE)
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    private List<Menu> childList = Lists.newArrayList();// 拥有子菜单列表

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="parent_id")
    @NotFound(action = NotFoundAction.IGNORE)
    @NotNull
    private Menu parent;	    // 父级菜单

    @ManyToMany(mappedBy = "menuList", fetch=FetchType.LAZY)
    @OrderBy("id") @Fetch(FetchMode.SUBSELECT)
    @NotFound(action = NotFoundAction.IGNORE)
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    private List<Role> roleList = Lists.newArrayList(); // 拥有角色列表

    @Length(min=1, max=255)
    private String parentIds;   // 所有父级编号

    @PrePersist
    public void prePersist(){
        //super.prePersist();
        this.id = IdGen.uuid();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHref() {
        return href;
    }

    public void setHref(String href) {
        this.href = href;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public String getIsShow() {
        return isShow;
    }

    public void setIsShow(String isShow) {
        this.isShow = isShow;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public List<Menu> getChildList() {
        return childList;
    }

    public void setChildList(List<Menu> childList) {
        this.childList = childList;
    }

    public List<Role> getRoleList() {
        return roleList;
    }

    public void setRoleList(List<Role> roleList) {
        this.roleList = roleList;
    }

    public Menu getParent() {
        return parent;
    }

    public void setParent(Menu parent) {
        this.parent = parent;
    }

    public String getParentIds() {
        return parentIds;
    }

    public void setParentIds(String parentIds) {
        this.parentIds = parentIds;
    }

    @Transient
    public static void sortList(List<Menu> list, List<Menu> sourcelist, String parentId){
        for (int i=0; i<sourcelist.size(); i++){
            Menu e = sourcelist.get(i);
            if (e.getParent()!=null && e.getParent().getId()!=null
                    && e.getParent().getId().equals(parentId)){
                list.add(e);
                // 判断是否还有子节点, 有则继续获取子节点
                for (int j=0; j<sourcelist.size(); j++){
                    Menu child = sourcelist.get(j);
                    if (child.getParent()!=null && child.getParent().getId()!=null
                            && child.getParent().getId().equals(e.getId())){
                        sortList(list, sourcelist, e.getId());
                        break;
                    }
                }
            }
        }
    }

    @Transient
    public boolean isRoot(){
        return isRoot(this.id);
    }

    @Transient
    public static boolean isRoot(String id){
        return id != null && id.equals("1");
    }
}
